package cn.lkx.eduservice.service.front.impl;

import cn.lkx.eduservice.entity.EduCourse;
import cn.lkx.eduservice.entity.EduTeacher;
import cn.lkx.eduservice.service.EduCourseService;
import cn.lkx.eduservice.service.EduTeacherService;
import cn.lkx.eduservice.service.front.IndexFrontService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 前台首页 服务实现类
 */
@Service
public class IndexFrontServiceImpl implements IndexFrontService {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduTeacherService teacherService;

    //查询排名前8的课程
    @Cacheable(value = "index",key = "'course'") //将结果存入缓存，value:命名空间 key为 index::course
    @Override
    public List<EduCourse> getIndexCourse() {
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.orderByDesc("view_count"); //按观看次数降序
        courseQueryWrapper.last("limit 8");
        List<EduCourse> courseList = courseService.list(courseQueryWrapper);
        return courseList;
    }
    //查询前4的讲师
    @Cacheable(value = "index",key = "'teacher'")
    @Override
    public List<EduTeacher> getIndexTeacher() {
        QueryWrapper<EduTeacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.orderByAsc("sort");
        teacherQueryWrapper.last("limit 4");
        List<EduTeacher> teacherList = teacherService.list(teacherQueryWrapper);
        return teacherList;
    }
}
